﻿/**********************************************
 * 功能名称：资源动因录入页面
编写人：  杨莹
建立时间： 杨 2008年11月26日
 * 更新时间： 2009年3月17日
说明：
本页面完成“领料人”与作业中心的对照。
 * 同步编码
 * 包括领用人、成本账户、材料编码
 * 
 * 录入后，提交保存。

 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Controllers;
using Hisun.Models;
using Hisun.UserControl;
using System.Text;

namespace Hisun.View.DataExchange
{
    public partial class Center_User_Meterial_Interface : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
       public string alter;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                int Totals = TotalPageAmount(TextBox_QueryValue.Text);
                int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
                Label_Total.Text = "共" + pages.ToString();
                Label_CurrenPageAndTo.Text = "页，当前第1页，转到";
                BindMyData(0, TextBox_QueryValue.Text);



                DropDownList_Center.Items.Clear();
                DropDownList_Center.Items.Add(new ListItem("未指定", ""));

                var myCenters = from a in db.TABC_JS_CostCenter
                                where new string[] { "BF", "BG", "BZ", "A" ,"M"}.Contains(a.类型)  orderby a.作业中心名称
                                select new
                                {
                                    a.作业中心ID,
                                    a.作业中心名称

                                };

                foreach (var myCenter in myCenters)
                {
                    DropDownList_Center.Items.Add(new ListItem(myCenter.作业中心名称, myCenter.作业中心ID.ToString()));

                }
          
            
            }
        }


        private void BindMyData(int PageIndex,string QueryValue)
        {

            int skipAccount = PageIndex * 10;

            if (QueryValue == "")
            {
                var MyQuery = (from a in db.TABC_DM_CostCenter_Ref_Username
                               join b in db.TABC_JS_CostCenter
                               on a.作业中心ID equals b.作业中心ID into mytemps
                               from mytemp in mytemps.DefaultIfEmpty()
                               select new
                               {
                                   a.ID,
                                   作业中心ID = (mytemp.作业中心ID == null ? "" : mytemp.作业中心ID.ToString()),
                                   a.领用人,
                                   作业中心名称 = (mytemp.作业中心名称 == null ? "" : mytemp.作业中心名称)

                               }).Skip(skipAccount).Take(10);
                GridView_CostCenter.DataSource = MyQuery;
                GridView_CostCenter.DataBind();


            }
            else
            {

                var MyQuery = (from a in db.TABC_DM_CostCenter_Ref_Username
                               join b in db.TABC_JS_CostCenter
                               on a.作业中心ID equals b.作业中心ID into mytemps
                               where a.领用人.Contains(QueryValue)
                               from mytemp in mytemps.DefaultIfEmpty()
                               select new
                               {
                                   a.ID,
                                   作业中心ID = (mytemp.作业中心ID == null ? "" : mytemp.作业中心ID.ToString()),
                                   a.领用人,
                                   作业中心名称 = (mytemp.作业中心名称 == null ? "" : mytemp.作业中心名称)

                               }).Skip(skipAccount).Take(10);
                GridView_CostCenter.DataSource = MyQuery;
                GridView_CostCenter.DataBind();

            
            
            }




        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount(string QueryValue)
        {
            int MyQuery = 0;
            if (QueryValue == "")
            {
                MyQuery = (from a in db.TABC_DM_CostCenter_Ref_Username select a).Count();
            }
            else
            {
                MyQuery = (from a in db.TABC_DM_CostCenter_Ref_Username   where a.领用人.Contains(QueryValue) select a).Count();
            
            }
           
            return MyQuery;
        }

        #region 实现翻页功能的代码段
        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount(TextBox_QueryValue.Text);



            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {

                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx,TextBox_QueryValue.Text);

        }



        #endregion


        protected void Button_Select_Command(object sender, CommandEventArgs e)
        {
            string MyID = e.CommandArgument.ToString();
            var myRow = (from a in db.TABC_DM_CostCenter_Ref_Username
                        where a.ID == Convert.ToInt32(MyID)
                        select a).Single();

            TextBox_UserName.Text = myRow.领用人;
            DropDownList_Center.SelectedValue = myRow.作业中心ID.ToString();

            Panel_Main.Visible = true;
            TextBox_UserName.ReadOnly = true;



        }


        #region 导入数据的代码
        protected void Button_ImportMeterial_Command(object sender, CommandEventArgs e)
        {
            string CurrentAccountPeriod = Application["accountPeriod"].ToString();
          //  string CurrentAccountPeriod = "200812";
            AbcSync sync = new AbcSync(CurrentAccountPeriod);

            if (sync.Meterial_Sync())
            {
                var DelRows = from a in db.TABC_CW_Meterial
                              where a.产品ID == null || a.产品ID.ToString() == ""
                              select a;
                db.TABC_CW_Meterial.DeleteAllOnSubmit(DelRows);
                db.SubmitChanges();

              
                alter = "<script>alert('导入材料领料信息成功');</script>";
            }
            else
            {
            
              
                alter = "<script>alert('导入材料领料信息失败');</script>";
            }
        }

        protected void Button_ImportUser_Command(object sender, CommandEventArgs e)
        {
            string CurrentAccountPeriod = Application["accountPeriod"].ToString();
            AbcSync sync = new AbcSync(CurrentAccountPeriod);

            if (sync.PlanPrice_Sync())
            {
                alter = "<script>alert('导入材料计划价格信息成功');</script>";
              
            }
            else
            {
               
                alter = "<script>alert('导入材料计划价格信息失败');</script>";
            }
        }

        protected void Button_MeterialDM_Click(object sender, EventArgs e)
        {
             string CurrentAccountPeriod = Application["accountPeriod"].ToString();
            AbcSync sync = new AbcSync(CurrentAccountPeriod);

            if (sync.MeterialBM_Sync())
            {

                
                alter = "<script>alert('同步材料编码成功');</script>";
            }
            else
            {
                
                alter = "<script>alert('同步材料编码失败');</script>";
            }
        }

        protected void Button_ProductAccount_Click(object sender, EventArgs e)
        {
             string CurrentAccountPeriod = Application["accountPeriod"].ToString();
            AbcSync sync = new AbcSync(CurrentAccountPeriod);

            if (sync.Product_JS_Sync())
            {

                alter = "<script>alert('同步产品账户编码成功');</script>";
            }
            else
            {
               
                alter = "<script>alert('同步产品账户编码失败');</script>";
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string CurrentAccountPeriod = Application["accountPeriod"].ToString();

            AbcSync sync = new AbcSync(CurrentAccountPeriod);

            if (sync.Meterial_P_Sync())
            {

                alter = "<script>alert('导入数据成功');</script>";
            }
            else
            {
                alter = "<script>alert('导入数据失败');</script>";
            }
        }

//该步骤在“原材料领料人”核对完毕，财务手工录入工作完毕后，执行。

        protected void Button_ImportMeterila2_Click(object sender, EventArgs e)
        {
            string CurrentAccountPeriod = Application["accountPeriod"].ToString();

            

            if (db.产品原材料导入(CurrentAccountPeriod) == 1)
            {

               
                alter = "<script>alert('导入数据成功');</script>";
            }
            else
            {
                alter = "<script>alert('导入数据失败');</script>";
            }
        }
        #endregion

        protected void Button_Save_Command(object sender, CommandEventArgs e)
        {
            var myEditRows = from a in db.TABC_DM_CostCenter_Ref_Username
                             where a.领用人 == TextBox_UserName.Text
                             select a;
            if (myEditRows.Count() == 1)
            {
                var myEditRow = myEditRows.Single();
                if (DropDownList_Center.SelectedValue != "")
                {
                    myEditRow.作业中心ID = Convert.ToInt32(DropDownList_Center.SelectedValue);

                }
                else
                {
                    myEditRow.作业中心ID = null;
                }
                
                myEditRow.编辑人ID = Session[ServerVars.sysSessionUserName].ToString();

                try
                {
                    db.SubmitChanges();
                    alter = "<script>alert('保存数据成功');</script>";
                
                    Panel_Main.Visible = false;
                    int pageIndx = Convert.ToInt32(CurrentPage.Value);
                    BindMyData(pageIndx, TextBox_QueryValue.Text);
                }
                catch
                {
                    alter = "<script>alert('保存数据失败');</script>";
              
                }
            }
            else if (myEditRows.Count() == 0)
            {
                TABC_DM_CostCenter_Ref_Username myNewRow = new TABC_DM_CostCenter_Ref_Username();
                myNewRow.领用人 = TextBox_UserName.Text;
          
                                    
                if (DropDownList_Center.SelectedValue != "")
                {
                    myNewRow.作业中心ID = Convert.ToInt32(DropDownList_Center.SelectedValue);

                }
                else
                {
                    myNewRow.作业中心ID = null;
                }

                myNewRow.编辑人ID = Session[ServerVars.sysSessionUserName].ToString();
                db.TABC_DM_CostCenter_Ref_Username.InsertOnSubmit(myNewRow);
                try
                {
                    db.SubmitChanges();

                    alter = "<script>alert('保存数据成功');</script>";
                    Panel_Main.Visible = false;
                    TextBox_UserName.ReadOnly = true;
                    int pageIndx = Convert.ToInt32(CurrentPage.Value);
                    BindMyData(pageIndx, TextBox_QueryValue.Text);
                }
                catch
                {
                    alter = "<script>alert('保存数据失败');</script>";
                }
            }
            

        }

        protected void Button_Cancel_Command(object sender, CommandEventArgs e)
        {
            TextBox_UserName.Text = "";
            DropDownList_Center.SelectedValue = "";
            Panel_Main.Visible = false;
            TextBox_UserName.ReadOnly = true;
        }

        protected void Button_Add_Click(object sender, EventArgs e)
        {
            TextBox_UserName.Text = "";
            DropDownList_Center.SelectedValue = "";
            Panel_Main.Visible = true;
            TextBox_UserName.ReadOnly = false;
        }







    }
}
